<template>
  <div p-5>
    <DynamicTable
      ref="dynamicTableRef"
      bordered
      :data-request="loadData"
      :columns="columns"
      row-key="heroid"
      :scroll="{
        y: 'calc(100vh - 360px)',
      }"
    >
      <template #toolbar>
        <a-button type="primary" @click="register"> 立案 </a-button>
      </template>
    </DynamicTable>
  </div>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import { cmColumns, type TableColumnItem } from './columns';
  import { useTable } from '@/components/core/dynamic-table';
  import { deleteCaseInfo, getCaseInfoList } from '@/api/supervision/case-management';
  import router from '@/router';
  import { Modal } from 'ant-design-vue';

  const [DynamicTable, dynamicTableInstance] = useTable();

  const register = () => {
    router.push({ path: '/administrative-penalty/register' });
  };

  const loadData = async (params): Promise<API.TableListResult> => {
    if (params.rangePicker && params.rangePicker !== null) {
      params.startTime = params.rangePicker[0];
      params.endTime = params.rangePicker[1];
      delete params['rangePicker'];
    }

    const data = await getCaseInfoList(params);
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve({
          list: data,
          ...params,
        });
      }, 500);
    });
  };

  /**
   * @description 表格删除行
   */
  const delRowConfirm = async (id: string) => {
    if (Array.isArray(id)) {
      Modal.confirm({
        title: '确定要删除所选的用户吗?',
        icon: '',
        centered: true,
        onOk: async () => {
          await deleteCaseInfo(id);
        },
      });
    } else {
      await deleteCaseInfo(id).finally(dynamicTableInstance?.reload);
    }
  };

  const columns: TableColumnItem[] = [
    ...cmColumns,
    {
      title: '操作',
      align: 'left',
      width: 140,
      dataIndex: 'ACTION',
      actions: ({ record }) => [
        {
          label: '查看',
          onClick: () =>
            router.push({
              path: '/administrative-penalty/caseView',
              query: { id: record.id },
            }),
        },
        {
          label: '编辑',
          ifShow: record.editShowFlag == 1,
          onClick: () =>
            router.push({
              path: '/administrative-penalty/register',
              query: { id: record.id },
            }),
        },
        {
          label: '处理',
          ifShow: record.handleShowFlag == 1,
          onClick: () =>
            router.push({
              path: '/administrative-penalty/caseHandle',
              query: { id: record.id },
            }),
        },
        {
          label: '删除',
          ifShow: record.deleteShowFlag == 1,
          popConfirm: {
            title: '你确定要删除吗？',
            onConfirm: () => delRowConfirm(record.id),
          },
        },
      ],
    },
  ];
</script>

<style lang="less" scoped></style>
